<template>
  <div class="page">
    <el-form label-width="80px" inline :model="searchFrom">
      <!-- <el-form-item label="问题分类">
        <el-select v-model="searchFrom.type" placeholder="请选择问题分类" clearable style="width:100%">
          <el-option v-for="item in typeList" :key="item.id" :label="item.question" :value="item.id" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="问题分类">
        <el-input v-model="searchFrom.question"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="questionTypeQuery">搜索</el-button>
        <el-button @click="show()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border>
      <el-table-column
        prop="question"
        label="分类名称"
        width="">
      </el-table-column>
      <el-table-column
        prop="remark"
        label="备注"
        width="">
      </el-table-column>
      <el-table-column
        label="操作"
        width="120">
        <template slot-scope="{row}">
          <el-button type="text" @click="show(row)">修改</el-button>
          <el-popconfirm
            title="确定删除吗？"
            @confirm="questionTypeDelete(row.id)"
          >
            <el-button slot="reference" type="text" style="color: #F56C6C;">删除</el-button>
          </el-popconfirm>
          
        </template>
      </el-table-column>
    </el-table>

    <!-- <el-pagination
      @size-change="pageSizeChange"
      @current-change="pageNoChange"
      :current-page.sync="page.pageNo"
      :page-size="page.pageSize"
      layout="total, prev, pager, next"
      :total="page.total">
    </el-pagination> -->

    <el-dialog
      :title="form.id ? '编辑' : '新增'"
      :visible.sync="dialogVisible"
      width="70%"
      :before-close="handleClose">
      <el-form label-position="right" label-width="80px" :model="form">
        <el-form-item label="问题分类">
          <el-input type="textarea" v-model="form.question" placeholder="请输入常见问题"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入问题回复"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button :loading="loading" type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { questionTypeUpdate, questionTypeAdd, questionTypeDelete, questionTypeQuery } from '@/api/official/official'
import { mapGetters } from 'vuex'

export default {
  computed: {
    ...mapGetters([
      'imagesUploadApi',
      'baseApi'
    ])
  },
  data() {
    return {
      searchFrom: {},
      page:{
        total:0,
        pageSize:10,
        pageNo:1,
      },
      tableData: [],
      typeList: [],
      form: {},
      dialogVisible: false,
      loading: false,
      deptList: []
    }
  },
  created() {
    this.questionTypeQuery()
  },
  methods: {
    questionTypeQuery() {
      questionTypeQuery({page: 1, size: 999, data: {}}).then(res => {
        this.tableData = res
      })
    },
    pageSizeChange(val){
      this.page.pageSize = val;
      this.questionTypeQuery();
    },
    pageNoChange(val){
      this.page.pageNo = val;
      this.questionTypeQuery();
    },
    show(row = {}) {
      this.dialogVisible = true
      this.form = {...row}
    },
    handleAvatarSuccess(res, file) {
      // this.$set(this.form, 'storagePath', this.baseApi + '/file/' + res.data.path)
      this.$set(this.form, 'storagePath', res.data.path)
      this.$set(this.form, 'storageId', res.data.uuid)
    },
    beforeAvatarUpload(file) {
      const supportedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/webp'];
      const isValidType = supportedTypes.includes(file.type);
      const isLt2M = file.size / 1024 / 1024 < 50;

      if (!isValidType) {
        this.$message.error('上传封面只能是图片格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传封面图片大小不能超过 50MB!');
      }
      return isValidType && isLt2M;
    },
    changeDept(dept) {
      this.form.deptName = this.deptList.find(item => item.id === this.form.deptCode).name
      this.form.branchCode = this.deptList.find(item => item.id === this.form.deptCode).branchCode
    },
    submit() {
      this.loading = true
      const data = {
        ...this.form
      }
      if (this.form.id) {
        questionTypeUpdate(data).then(res => {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.questionTypeQuery()
        }).finally(() => {
          this.loading = false
          this.dialogVisible = false
        })
      } else {
        questionTypeAdd(data).then(res => {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.dialogVisible = false
          this.questionTypeQuery()
        }).finally(() => {
          this.loading = false
          this.dialogVisible = false
        })
      }
    },
    questionTypeDelete(id) {
      questionTypeDelete([id]).then(res => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.questionTypeQuery()
      })
    }
  }
}
</script>

<style scoped>
.page {
  padding: 24px;
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9 !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  border-radius: 0;
}
</style>
